<?php
/**
 * 学生信息的修改和添加
 */
include "../util/DBUtil.php";

$action = $_GET['flag'];//记录操作行为
$id = $_GET['id'];
$db = new DBUtil();
if ($action == 0) {//修改学生信息
    $sql = "select tb_student.*,tb_grade.grade from tb_student,tb_grade where tb_student.grade_id=tb_grade.id and tb_student.id=?";//查询学生信息
    $params = array($id);
    $stu = $db->query($sql, $params)[0]; //将结果集存到stu变量中
//    print_r($stu);
    $sql = "select * from tb_grade ";//查询所有班级
    $grade = $db->query($sql);  //将所有班级存到grade变量中
} elseif ($action == 1) {//删除学生
    $sql = "delete from tb_score where student_id=?";
    $params = array($id);
    $db->delete($sql,$params);
    $sql ="delete from tb_student where id=?";
    $db->delete($sql,$params);
    echo "<script>alert('删除成功');window.open('student.php','info')</script>";
    return;
}
?>
<form action="../action/stu_formOK.php" method="post">
    <input type="hidden" name="id" value="<?php echo $id ?>">
    <table>
        <tr>
            <td>学号：</td>
            <td><input type="text" name="stuNo" value=<?php echo $action == 0 ? $stu['student_no'] : '' ?> <?php echo $action==''? '':'disabled' ?> ></td>
        </tr>
        <tr>
            <td>姓名：</td>
            <td><input type="text" name="name" value="<?php echo $action == 0 ? $stu['name'] : '' ?>"></td>
        </tr>
        <tr>
            <td>性别：</td>
            <td>
                <input type="radio" name="gender" value="男" checked>男
                <input type="radio" name="gender" value="女" <?php if ($action == 0) {
                    echo $stu['gender'] == '女' ? 'checked' : '';
                } ?>>女
            </td>
        </tr>
        <tr>
            <td>班级：</td>
            <td>
                <select name="grade" id="">
                    <?php foreach ($grade as $item) {
                        ?>
                        <option value="<?php echo $item['id'] ?>" <?php echo $stu['grade_id'] == $item['id'] ? 'selected' : '' ?>><?php echo $item['grade'] ?></option>
                        <?
                    } ?>
                </select>
            </td>
        </tr>
    </table>
    <button type="submit">确认</button>
</form>
